#include<bits/stdc++.h>
using namespace std;
int T,n,a[200005],maxx=-1;
int fi(string s,char x,int pos){
    for(int i=pos-1;i>=1;i--){
        if(s[i]==x){
			if(a[i]==a[pos]){
				return a[i];
			}else return -1;
		}
    }
    return -1;
}
int ad(string s){
    int sum=0;
    for(int i=2;i<=s.size();i++){
        int pos=fi(s,s[i],i);
        if(pos!=-1){
			sum+=pos;
		}
    }
    return sum;
}
void work(int dep,string s){
    if(dep>n){
        s=" "+s;
        maxx=max(maxx,ad(s));
        return;
    }
    work(dep+1,s+'a');
    work(dep+1,s+'b');
}
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>T;
    while(T--){
        maxx=-1;
        cin>>n;
        for(int i=1;i<=n;i++)cin>>a[i];
        work(1,"");
        cout<<maxx<<"\n";
    }
    return 0;
}
